package com.yycx.module.bus.provider.handler.haiying.settle.pay;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.ExportInterceptor;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.admin.provider.mapper.BaseUserDeptMapper;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.BusDeptService;
import com.yycx.module.bus.provider.service.BusSalemanService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 结算申请单
 *
 * @author zyf
 */
@Component
public class SettlePayTab2PageListHandler implements PageInterceptor<BusPayment>, ExportInterceptor {


    @Autowired
    private TaskService taskService;

    @Autowired
    private BusDeptService deptService;

    @Override
    public void prepare(CriteriaQuery<BusPayment> cq, PageParams pageParams, EntityMap params) {
        String paymentNo = params.get("paymentNo");
        String saleManName = params.get("saleManName");
        OpenUser openUser = OpenHelper.getUser();
        Long companyId = params.getLong("companyId", openUser.getCompanyId());
        cq.select(BusPayment.class, "*");
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        cq.addSelect(BusSaleman.class, "saleManName", "saleManBusinessNo");
        cq.eq(true, "amountType", AmountTypeEnum.Settle.getCode());
        cq.eq(true, "payment.companyId", companyId);
        cq.like("payment.paymentNo", paymentNo);
        cq.like("saleman.saleManName", saleManName);
        cq.ge(FlymeUtils.isNotEmpty(beginDate), "amountDate", beginDate);
        cq.le(FlymeUtils.isNotEmpty(endDate), "amountDate", endDate);
        String userName = openUser.getUsername();
        if (!userName.contains("admin")) {
            //非管理员只查看自己申请的数据
            cq.eq("payment.userId", openUser.getUserId());
        }
        cq.createJoin(BusSaleman.class);
        cq.orderByDesc("payment.createTime");
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<BusPayment> cq, List<EntityMap> result, EntityMap extra) {
        String companyName = OpenHelper.getAttr("companyName", String.class);
        for (EntityMap entityMap : result) {
            entityMap.put("companyName", companyName);
            String processInstanceId = entityMap.get("processInstanceId");
            Long deptId = entityMap.getLong("deptId");
            if (FlymeUtils.isNotEmpty(deptId)) {
                BusDept dept = deptService.getById(deptId);
                entityMap.put("deptName", dept.getDeptName());
            }
            if (FlymeUtils.isNotEmpty(processInstanceId)) {
                //查询当前审批节点
                List<Task> task = taskService.createTaskQuery().processInstanceId(processInstanceId).active().list();
                if (FlymeUtils.isNotEmpty(task)) {
                    if (task.size() == 1) {
                        entityMap.put("taskName", task.get(0).getName());
                        entityMap.put("taskId", task.get(0).getId());
                        entityMap.put("taskDefinitionKey", task.get(0).getTaskDefinitionKey());
                    }
                    if (task.size() > 1) {
                        entityMap.put("taskName", "商务中心核对");
                    }
                }
            }
        }
        return result;
    }

}
